import 'package:enterprise_v6_app/app/components/toast_widget.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/core/utils/user_manager.dart';
import 'package:enterprise_v6_app/app/data/common/simple_choice_bean.dart';
import 'package:enterprise_v6_app/app/data/entities/closure/closure_appointment_entity.dart';
import 'package:enterprise_v6_app/app/modules/task/task_all/task_closure/task_waiting_management/task_waiting_management_controller.dart';
import 'package:get/get.dart';
import 'package:jiffy/jiffy.dart';

class WaitingManagementApprovalController extends GetxController {
  /// 外部列表controller
  final taskController = Get.find<TaskWaitingManagementController>();

  /// 审批实体类
  ClosureAppointmentEntity entity = Get.arguments as ClosureAppointmentEntity;

  /// 车辆外观选择
  List<SimpleChoiceBean> vehicleAppearanceList = [];

  /// 驾驶证
  List<SimpleChoiceBean> driverSLicenseList = [];

  /// 行驶证
  List<SimpleChoiceBean> drivingLicenseList = [];

  /// 货物状态
  List<SimpleChoiceBean> cargoStatusList = [];

  @override
  void onInit() {
    super.onInit();
    vehicleAppearanceList = [
      SimpleChoiceBean(name: '完好', id: '0', isChecked: true),
      SimpleChoiceBean(name: '破损', id: '1'),
      SimpleChoiceBean(name: '其他', id: '2'),
    ];
    driverSLicenseList = [
      SimpleChoiceBean(name: '有效中', id: '0', isChecked: true),
      SimpleChoiceBean(name: '缺失', id: '1'),
      SimpleChoiceBean(name: '到期', id: '2'),
    ];
    drivingLicenseList = [
      SimpleChoiceBean(name: '有效中', id: '0', isChecked: true),
      SimpleChoiceBean(name: '缺失', id: '1'),
      SimpleChoiceBean(name: '到期', id: '2'),
    ];
    cargoStatusList = [
      SimpleChoiceBean(name: '完好', id: '0', isChecked: true),
      SimpleChoiceBean(name: '破损', id: '1'),
      SimpleChoiceBean(name: '其他', id: '2'),
    ];
  }

  /// 通过或者驳回
  Future<void> passOrReject(bool isPass) async {
    if (entity.vehicleAppearance.isEmpty) {
      AppToast.showWarning('请上传车辆外观图片!');
      return;
    }
    if (entity.driverLicense.isEmpty) {
      AppToast.showWarning('请上传驾驶证图片!');
      return;
    }
    // 候检状态 1:候检中 2:已准入 3:拒绝
    entity.waitingForInspectionStatus = isPass ? 2 : 3;
    // 设置检查人
    entity.inspectedBy = UserManager.getUser()!.systemUser!.nickname ?? '';
    // 设置检查人id
    entity.inspectedById = UserManager.getUserId() ?? '';
    // 设置检查时间
    entity.inspectedTime = Jiffy.parseFromDateTime(DateTime.now()).format(pattern: 'yyyy-MM-dd HH:mm:ss');
    // 获取列表接口
    final res = await HttpUtil()
        .generatePostRequest<List<ClosureAppointmentEntity>>(
          Interface.updateReservation,
          data: entity.toJson(),
        )
        .execute();
    res.onSuccess((result) {
      taskController.refreshPage();
      Get.back();
      AppToast.showWarning('检查成功!');
    });
  }
}
